package netty.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: zhangrongqing
 * @Date: 2020/1/6 16:25
 */
public class FileChannelDemo2 {
    public static void copyFile() throws IOException {
        FileInputStream inputStream = new FileInputStream("C:\\Users\\zhang.rongqing\\Desktop\\test.txt");
        FileChannel fileChannel01 = inputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\zhang.rongqing\\Desktop\\test2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        while(true){
            byteBuffer.clear();
            int read = fileChannel01.read(byteBuffer);
            System.out.println(read);
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
            if(read == -1){
                break;
            }
        }
        inputStream.close();
        fileOutputStream.close();
    }

    public static void main(String[] args) {
        try {
            FileChannelDemo2.copyFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
